#include<stdio.h>
#include<string.h>

#define Min(a,b) (a)<(b)?(a):(b)

int MyStrlen(const char *s)
{
    int count = 0;
    while(*s != '\0')
    {
        count++;
        s++;

    }
    return count;
}


int MyStrcmp(const char*s1,const char*s2)
{
 
    while(*s1 == *s2 && *s1 !='\0' && *s2 != '\0')
    {
       s1++;
       s2++;
    }
    // 不相等出循环 或者 \0
    return *s1 - *s2;
}



int MyStrncmp(const char *s1,const char *s2,int n)
{
   n = Min (Min(MyStrlen(s1),MyStrlen(s2)), n ); //取三者的最小值  
   
   while(*s1 == *s2 && n !=1 )
   {
     *s1++;
     *s2++;
      n--;
   }
   return *s1-*s2;
   
}
   
   
void MyStrcpy( char *dest,const char *src)
{
    while(*src != '\0')
    {
        *dest++ = *src++;  //  简化成 *dest++ = *src++;
        *dest = '\0';
    }
} 
   
 char* MyStrncpy( char *dest ,const char *src , int n)
 {
    while(n--!=0)
    {
        *dest++ = *src++;  //  简化成 *dest++ = *src++;
         *dest = '\0';
    }
 }




void MyStrcat(char *dest, const char *src)
{ 
    MyStrcpy(dest + MyStrlen(dest),src);
}


void MyStrncat(char *dest, const char *src,int n)
{
    MyStrncpy(dest + MyStrlen(dest),src,n);
}



const char * MyStrstr(const char *dest,const char *src)    //滑动窗口算法
{
    int lenDest = MyStrlen(dest);
    int lenSrc  = MyStrlen(src);
    
    for(int i=0;i <= lenDest-lenSrc;i++)
    {
        if(MyStrncmp(dest + i,src,lenSrc) == 0)
        {
            return dest+i;
        }    
    }
    return NULL;
}






char * MyStrtok(char *src,const char *delim)
{
    //上次切割完的下一个字符串的首地址
    static char *nextStr = NULL;
    
    if(src == NULL)   //想继续切割上一次的字符串
        src = nextStr;
    
    if(src == NULL)  //上次切的字符串切完了
        return 0;
    
    // 跳过所有分隔符

    
    while(*src != '\0')
    {
        //判断当前字符是否是分隔符
        char tmp[] = {*src,'\0'};
        if(MyStrstr(delim,tmp))//当前字符是分隔符
        {
            src++;
        }
        else 
        {
            break;// 找到非分隔符的有效字符，结束循环
        }
    }
    if( *src == '\0')
        return NULL;
    //src 就是下一个有效字符串的首地址
    
    char *end = src;
    //找下一个分隔符
    while(*end != '\0')
    {
         char tmp[] = {*end,'\0'};
        if(MyStrstr(delim,tmp))
        {
           break;
        }
        else 
        {
            end++;// 继续找下一个有效字符
        }
    }
    if(*end == '\0')
    {
        nextStr = NULL;
    }
    else
    {
        *end = '\0';
        
        nextStr = end + 1;
    }

    return src;
}

int main()
{
    
    // // printf("%d\n",MyStrlen("hello"));
    // char s1[20] = "hello";
    // char s2[21] = "world";
    // MyStrncat(s1,s2,2);
    // // printf("%s\n",s1); 
    // char s1[] = "helloworld";
    // char s2[] = "ll";
   
    
    // char* dest = MyStrstr(s1,s2);  // 在 s1 里寻找 s2
    
    // if (dest)//dest不为空
    // {
    //     printf("%s\n",dest);
    // }
    // else
    // {
    //     printf("没找到\n");
    // }

    char s[] = "zhangsan,lisi.wangwu, .sad";
    char *p = strtok(s,",.");
    while(p != NULL)
    {
        printf("%s\n",p);
        p = strtok(NULL,",.");
    }
    
    return 0;
}